$(function () {
    initCalendar();
    getSignData();
    autoSign();

    //自动签到
    function autoSign(){
        var isSign = $('#isSign').val();
        if (isSign=='false'){
            saveSignData();
            $('body').on('click', '#sign-success-pic', function () {
                window.history.back()
            })
        }
    }
    $('body').on('click', '#sign-button', function () {
        saveSignData();
    });

    // 签到日历
    function initCalendar() {
        $('#lecture-date-bx').calendar({
            onMonthYearChangeEnd: function (o, year, month) {
                getSignData(year, month + 1);
            }
        })
    }
    // 获取签到数据
    function getSignData(year, month) {
        $.ajax({
            url: baseloaction + '/mobile/uc/sign/signData',
            data: {'year': year, 'month': month},
            dataType: 'json',
            success: function (response) {
                if (response.success==true) {
                    var entity = response.entity;
                    $('#month-sign-count').text(entity.monthSign);
                    var objMap = entity.objMap;
                    if (objMap !=null) {
                        $('.picker-calendar-month-current .picker-calendar-day').each(function (i, v) {
                            var year = JSON.stringify($(v).data('year'));
                            var month = JSON.stringify($(v).data('month'));
                            var day = JSON.stringify($(v).data('day'));
                            if(parseInt(month) <9){
                                month = '0'+(parseInt(month)+1);
                            }else {
                                month = (parseInt(month)+1);
                            }
                            if(day.length <2){
                                day = '0'+day;
                            }
                            var str = year +'-'+month+'-'+day;
                            var val = objMap[str];
                            if(typeof val !='undefined'){
                                $(v).addClass('active').html('<span>签到</span>');
                            }
                        });
                    } else {
                        $('#month-sign-count').text(0);
                    }
                }
            }
        })
    }

    // 签到
    function saveSignData() {
        $.ajax({
            url: baseloaction + '/mobile/uc/sign/sign',
            dataType: 'json',
            success: function (response) {
                if (response.success) {

                    var signDate = new Date(response.entity.signDate);
                    signDate.setMonth(signDate.getMonth() - 1);
                    signDate = signDate.toLocaleDateString('zh-CN').replace(/\//g, '-');
                    $('div.picker-calendar-day').each(function (i, v) {
                        if ($(v).data('date') == signDate) {
                            $(v).addClass('active').html('<span>签到</span>');
                        }
                    });
                    increase('#month-sign-count');
                    increase('#sign-total');
                    $('#sign-button').html("已签到").attr("id", '');

                    $.alert($('#sign-modal-ele').val(), function () {
                        window.history.back()
                    })
                    $("#sigDay").html(response.entity.day)
                    $('.modal-in').find('.modal-button-bold').text('知道了').attr('style', '');
                } else {
                    var message = response.message;
                    if ('LOGIN' == message) {
                        window.location = baseloaction + '/mobile/login';
                    } else {
                        $.toast(message);
                    }
                }
            },
            error: function () {
                $.toast('系统繁忙，请稍后再试');
            }
        });
        // 签到成功后增加签到数
        function increase(selector) {
            var html = $(selector).html();
            var regex = /[\d]+/g;
            var number = parseInt(regex.exec(html));
            number += 1;
            $(selector).html(html.replace(regex, number));
        }
    }
});